
import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class User {

    public User(int id, String name, String pass) {
        this.username = name.toLowerCase();
        this.pwd = pass;
        this.id = id;
    }

    public User(String name, String pass) {
        this.username = name.toLowerCase();
        this.pwd = pass;
    }

    public void create() {
    }

    public boolean auth(String pass) {
        return pass.equals(pwd);
    }

    public String toString() {
        return "[" + id + "," + username + "," + pwd + "]";
    }
    //Static methos

    public static void main(String args[]) throws Exception {
        init();
        writeUser(new User("juan", "123456"));
        writeUser(new User("juan2", "123456"));
        writeUser(new User("juan3", "123456"));
        writeUser(new User("j", "1"));
        System.out.println(get("j23uan"));
        writeUser(new User("j", "2"));
        writeUser(new User("j", "3"));
    }

    public static User get(String name) {
        return userIndex.get(name);
    }

    public static void printUsers() {
        Enumeration e = userIndex.keys();
        while(e.hasMoreElements())
            System.out.println(e.nextElement());
    }

    private static User readUser(int ln) {
        try {
            users.seek(ln * MAX_LEN);
            String u = users.readLine();
            int i = u.indexOf(",");
            if(i == -1)
                return null;
            User ret = new User(ln, u.substring(0, i), (u.substring(i + 1)));
            //System.out.println(ret);
            return ret;
        } catch (Exception e) {
            //System.out.println(e);
            return null;
        }
    }

    static public boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }



    public static boolean deleteUser(String username) {
        try {
            User u = userIndex.remove(username);
            users.seek(u.id * MAX_LEN);
            users.writeBytes("\n\n\n");
            File dir = new File(Server.getRootDir() + "/" + u.username);
            return deleteDirectory(dir);
            
        } catch(Exception e){
            return false;
        }
    }

    public static boolean writeUser(User u) {
        try {
            User existent = userIndex.get(u.username);
            if (existent != null) {
                u.id = existent.id;
            } else {
                u.id = userIndex.size();
                File dir = new File(Server.getRootDir() + "/" + u.username);
                if (dir.exists())
                    deleteDirectory(dir);
                dir.mkdir();
            }

            users.seek(u.id * MAX_LEN);
            users.writeBytes(u.username + "," + u.pwd + "\n");
            userIndex.put(u.username, u);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean init() {
        try {
            users = new RandomAccessFile("users.data", "rws");

            for (int i = 0; i <= (int) (users.length() / MAX_LEN); i++) {
                User u = readUser(i);
                if (u != null) {
                    userIndex.put(u.username, u);
                    File dir = new File(Server.getRootDir() + "/" + u.username);
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    //
    int id = -1;
    String username = null;
    String pwd = null;
    //static vars
    private static Hashtable<String, User> userIndex = new Hashtable<String, User>();
    private static final int MAX_LEN = 32;
    private static RandomAccessFile users;
}
